<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt"  :value="inputValue" @change="handleInputChange"/>
    <a-button type="primary" @click="addTodoList">提交</a-button>

    <a-list bordered :dataSource="List" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item" >
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @change="e=>changeBox(e,item.id)">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="deleteTodoListById(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneLength}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button :type="viewKey==='all'?'primary':'default'"
          @click="changeList('all')">全部</a-button>
          <a-button :type="viewKey==='undone'?'primary':'default'"  @click="changeList('undone')" >未完成</a-button>
          <a-button :type="viewKey==='done'?'primary':'default'" @click="changeList('done')">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clear">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
// 按需导入函数 获取vuex中的state的值
import {mapState,mapGetters} from 'vuex'
export default {
  name: 'app',
  data() {
    return {
    }
  },
  methods:{
    handleInputChange(e){
    // console.log(e.target.value);
// 调用mutations中的setInputValue函数 更改infoList
this.$store.commit('setInputValue',e.target.value)
    },
    // 新增item项
   addTodoList(){
    if(this.inputValue.trim().length<=0){
      return this.$message.warning('禁止为空！')
    }
    // 调用mutations中的方法 修改infoList数组
    this.$store.commit('addItem')
    },

    deleteTodoListById(id){
      this.$store.commit('deleteItem',id)
    },
    // 监听复选框选中状态的函数
    changeBox(e,id){
       const params={
      id,
      status:e.target.checked
    }
      this.$store.commit('changeCheckBox',params)
    },
    // 清理已完成
    clear(){
    this.$store.commit('clearDone')
    },
    changeList(key){
this.$store.commit('changeViewKey',key)
    }
  },
  created(){
    // **原始调用** - $store
    this.$store.dispatch('getList')
  },
  computed:{
    // mapState是辅助函数，帮助我们把store中的数据映射到 组件的计算属性中, 它属于一种方便用法
   
   ...mapState(['infoList','inputValue','viewKey']) ,
   ...mapGetters(['unDoneLength','List'])
  }
  };
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>